package com.kevin.rocketmq;

import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author Kevin Liu
 * @date 2020/10/21 11:37 上午
 */
@Component
@RequiredArgsConstructor
public class Sender {

    private RocketMQTemplate rocketMqTemplate;

    public void sendMsg(){
        rocketMqTemplate.convertAndSend("test-topic-1", "Hello, World!");
        //send spring message
        rocketMqTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
        //send message asynchronously
        rocketMqTemplate.asyncSend("test-topic-2", "Hello", new SendCallback() {
            @Override
            public void onSuccess(SendResult var1) {
                System.out.printf("async onSuccess SendResult=%s %n", var1);
            }

            @Override
            public void onException(Throwable var1) {
                System.out.printf("async onException Throwable=%s %n", var1);
            }

        });
        //Send messages orderly
        rocketMqTemplate.syncSendOrderly("orderly_topic",MessageBuilder.withPayload("Hello, World").build(),"hashkey");

        rocketMqTemplate.sendOneWay("test-topic-1", "Hello, World!");
    }


}
